//
//  Global.swift
//  WearDu
//
//  Created by wang tiao on 2025/3/19.
//

import UIKit
import JKSwiftExtension
import AVFoundation
import UniformTypeIdentifiers
import CryptoKit
import CommonCrypto

 
struct LoadError: Error {
    let code: Int
    let message: String?
}
 
func printf(_ items: Any...) {
    struct Static {
        static let dateFormatter: DateFormatter = {
            let formatter = DateFormatter()
            formatter.dateFormat = "yyyy-MM-dd HH:mm:ss.SSSSSS"
            if let timeZone = TimeZone(identifier: "Asia/Beijing") {
                formatter.timeZone = timeZone
            }
            return formatter
        }()
    }
    
    let timeString = Static.dateFormatter.string(from: Date())
    let message = items.map { "\($0)" }.joined(separator: " ")
    print("\(timeString) \(message)")
}


func UIFontMake(_ size: CGFloat) -> UIFont {
    return UIFont.systemFont(ofSize: size)
}

func UIFontMediumMake(_ size: CGFloat) -> UIFont {
    return UIFont.systemFont(ofSize: size, weight: .medium)
}

func UIFontSemiboldMake(_ size: CGFloat) -> UIFont {
    return UIFont.systemFont(ofSize: size, weight: .semibold)
}

func UIFontBoldMake(_ size: CGFloat) -> UIFont {
    return UIFont.systemFont(ofSize: size, weight: .bold)
}

func UIColorMake(_ hex: Int) -> UIColor {
    return UIColor(hexInt: hex)
}

func UIColorMake(_ hexString: String) -> UIColor {
    return UIColor.hexStringColor(hexString: hexString)
}

func calculateTextSize(text: String, font: UIFont, maxWidth: CGFloat) -> CGSize {
    let attributes: [NSAttributedString.Key: Any] = [.font: font]
    let rect = text.boundingRect(with: CGSize(width: maxWidth, height: CGFLOAT_MAX),
                                 options: .usesLineFragmentOrigin,
                                 attributes: attributes,
                                 context: nil)
    return CGSizeMake(ceil(rect.size.width), ceil(rect.size.height))
}

func topNavigationController() -> UINavigationController? {
    return topViewController()?.navigationController
}

func topViewController() -> UIViewController? {
    return findTopViewController(from: getMainWindow()?.rootViewController)
}

private  func findTopViewController(from rootVC: UIViewController?) -> UIViewController? {
    guard let rootVC = rootVC else { return nil }
    
    if let presentedVC = rootVC.presentedViewController {
        return findTopViewController(from: presentedVC)
    }
    
    if let tabBarController = rootVC as? UITabBarController {
        return findTopViewController(from: tabBarController.selectedViewController)
    }
    
    if let navController = rootVC as? UINavigationController {
        return findTopViewController(from: navController.topViewController)
    }
    return rootVC
}

func getRootController() -> UIViewController? {
    return getMainWindow()?.rootViewController
}

func getMainWindow() -> UIWindow? {
    if #available(iOS 13.0, *) {
        return UIApplication.shared.windows.first { $0.isKeyWindow }
    } else {
        return UIApplication.shared.keyWindow
    }
}

 
func FourCharCodeToString(fourCharCode: FourCharCode) -> String {
    let n = Int(fourCharCode)
    let c1 = Character(UnicodeScalar((n >> 24) & 255)!)
    let c2 = Character(UnicodeScalar((n >> 16) & 255)!)
    let c3 = Character(UnicodeScalar((n >> 8) & 255)!)
    let c4 = Character(UnicodeScalar(n & 255)!)
    return "\(c1)\(c2)\(c3)\(c4)"
}

func convertTimeSecond(_ timeSecond: Int) -> String {
    guard timeSecond >= 0 else {
        return "00:00"
    }
    
    let hours = timeSecond / 3600
    let minutes = (timeSecond % 3600) / 60
    let seconds = timeSecond % 60
    
    if timeSecond < 60 {
        return String(format: "00:%02d", seconds)
    } else if timeSecond < 3600 {
        return String(format: "%02d:%02d", minutes, seconds)
    } else {
        return String(format: "%02d:%02d:%02d", hours, minutes, seconds)
    }
}

func getDescription(_ model: Any) -> String {
    let mirror = Mirror(reflecting: model)
    let typeName = String(describing: type(of: model))
    var propertyPairs: [String] = []
    for child in mirror.children {
        if let label = child.label {
            propertyPairs.append("\(label): \(child.value)")
        }
    }
    
    let result = "\(typeName)(\(propertyPairs.joined(separator: "\n")))"
    return result
}

func printProperties(_ model: Any) {
    print(getDescription(model))
}

 
